home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj1291.zip / STAT.ZIP / SOURCE.ZIP / PATICK.ASM < prev    next >
Assembly Source File  |  1990-05-09  |  6KB  |  238 lines

  1. TITLE    patick - IBM PC / Clone Clock Tick CS:IP Grabber
  2. ;
  3. ; File:        patick.asm
  4. ;
  5. ;        Copyright 1990
  6. ;        Fred Motteler and Applied Microsystems Corporation
  7. ;        All Rights Reserved
  8. ;
  9. ; Description:
  10. ;
  11. ;    This file contains three functions:
  12. ;
  13. ;    C callable:
  14. ;
  15. ;    void painit(bufferLP, lengthN)    Initialize grabber interrupt vector
  16. ;    int paclose()            Close grabber interrupt vector
  17. ;
  18. ;    Interrupt routine, this is treated like part of painit():
  19. ;
  20. ;    patick                Grab CS:IP value
  21. ;
  22. ;    These functions are configured for small model.
  23. ;
  24. ;    Stack frame structure for painit():
  25. ;    
  26. stkfr    STRUC
  27.     OLD_FR    DW    ?    ; Previous stack frame pointer
  28.     RETADDR    DW    ?    ; Return address to caller
  29.     BUFFERP    DW    ?    ; Pointer to buffer to use
  30.     BUFLEN    DW    ?    ; Length of buffer (in longwords)
  31. stkfr    ENDS
  32. ;
  33. ;    Stack frame structure for clock tick timer routine.
  34. ;
  35. intfr    STRUC
  36.     INT_FR    DW    ?    ; Pre-interrupt stack frame pointer
  37.     IP_VAL    DW    ?    ; Pre-interrupt IP value
  38.     CS_VAL    DW    ?    ; Pre-interrupt CS value
  39. intfr    ENDS
  40.  
  41. TIMER    EQU    8h        ; Timer interrupt vector number
  42.  
  43. DGROUP  GROUP    _DATA
  44. _DATA    SEGMENT    WORD PUBLIC 'DATA'
  45.         ASSUME    DS:DGROUP
  46.  
  47. bufptr    DW    0        ; Starting point of buffer
  48. bufsiz    DW    0        ; Number of longwords in the buffer
  49. bufindx    DW    0        ; Next location of buffer to use
  50. bufwrap    DB    0        ; Flag if buffer has wrapped...
  51.  
  52. _DATA    ENDS
  53.  
  54. _TEXT    SEGMENT    BYTE PUBLIC 'CODE'
  55.     ASSUME    CS:_TEXT
  56. ;
  57. ;    void paopen (unsigned long *bufferLP, int lengthN)
  58. ;
  59. ;    This a C callable function to initialize the CS:IP grabber and
  60. ;    start it up.  bufferLP points the buffer of where to write CS:IP
  61. ;    values.  lengthN is the length of the buffer in longwords.
  62. ;
  63.     PUBLIC    paopen
  64. paopen    PROC    NEAR
  65.     push    bp
  66.     mov    bp,sp
  67.     push    si
  68.     push    di
  69.     push    es
  70. ;
  71. ;    Set up the local buffer pointer values from those passed on the
  72. ;    stack.
  73. ;
  74.     mov    ax,[bp].BUFFERP    ; Get pointer to start of buffer
  75.     mov    bufptr,ax
  76.     mov    ax,[bp].BUFLEN    ; Get length of the buffer
  77.     shl    ax,1        ; convert longword length to byte length
  78.     shl    ax,1
  79.     mov    bufsiz,ax
  80.     xor    ax,ax        ; Start at the beginning of the buffer
  81.     mov    bufindx,ax
  82.     mov    bufwrap,al    ; Reset buffer wrap flag
  83. ;
  84. ;    Save the original clock tick interrupt vector.
  85. ;
  86.     mov    al,TIMER    ; interrupt number into al
  87.     mov    ah,35h        ; DOS function = get vector 
  88.     int    21h        ; DOS returns old vector in es:bx
  89.     mov    cs:oldseg,es    ; save old segment
  90.     mov    cs:oldoff,bx    ; save old offset
  91. ;
  92. ;    Disable interrupts while changing the interrupt vector.
  93. ;
  94.     cli
  95. ;
  96. ;    Change the clock tick interrupt routine to point at local interrupt
  97. ;    routine.
  98. ;
  99.     mov    al,TIMER    ; vector number
  100.     mov    ah,25h        ; DOS function = set vector 
  101.     mov    dx,OFFSET patick ; point to our interrupt handler
  102.     push    ds        ; don't lose ds, we need to get to local data 
  103.     push    cs        ; move this cs to ds
  104.     pop    ds        ; 
  105.     int    21h        ; set the new vector
  106.     pop    ds        ; restore ds
  107. ;
  108. ;    Enable interrupts and return;
  109. ;
  110.     pop    es
  111.     pop    di
  112.     pop    si
  113.     pop    bp
  114.     sti
  115.     ret
  116. ;
  117. ;    Clock tick grabber routine
  118. ;
  119. ;    This routine samples the CS:IP that were pushed on to the stack
  120. ;    when the interrupt occurs.
  121. ;
  122. patick:    push    bp
  123.     mov    bp,sp        ; Treat CS:IP values like stack frame
  124.     push    ax
  125.     push    bx
  126.     push    ds
  127. ;
  128. ;    Get the local ds to allow access to local variables.
  129. ;
  130.     mov    ax,DGROUP
  131.     mov    ds,ax
  132. ;
  133. ;    Use bx as a pointer to the recording buffer
  134. ;
  135.     mov    bx,bufptr
  136.     add    bx,bufindx
  137. ;
  138. ;    Grab the pre-interrupt CS:IP values off the stack
  139. ;
  140.     mov    ax,[bp].IP_VAL    ; grab the IP
  141.     mov    [bx],ax        ; save the IP in the recording buffer
  142.     inc    bx
  143.     inc    bx
  144.     mov    ax,[bp].CS_VAL    ; grab the CS
  145.     mov    [bx],ax        ; save the CS in the recording buffer
  146.     inc    bx
  147.     inc    bx
  148. ;
  149. ;    Check if we are at the end of the buffer
  150. ;
  151.     sub    bx,bufptr    ; get the byte offset index back again
  152.     mov    ax,bufsiz    ; get the buffer byte length
  153.     cmp    ax,bx
  154.     jne    notend        ; jump if not at the end of the buffer
  155. ;
  156. ;    At the end of the buffer
  157. ;
  158.     mov    bx,0        ; reset the buffer index
  159.     mov    al,0ffh        ; set flag to indicate buffer wrap
  160.     mov    bufwrap,al
  161. ;
  162. ;    Write out modified buffer index
  163. ;
  164. notend:    mov    bufindx,bx
  165. ;
  166. ;    Clean up
  167. ;
  168.     pop    ds
  169.     pop    bx
  170.     pop    ax
  171.     pop    bp
  172. ;
  173. ;    Jump to the original interrupt service routine.  An immediate jump
  174. ;    is used so no segment registers are required.
  175. ;
  176.     DB    0eah        ; jmp immediate, to the offset:segment
  177. ;                  selected below (brute force approach).
  178. ;
  179. ;    Original interrupt handler's offset and segment values.  These are
  180. ;    in the current code segment to allow the interrupt routine given
  181. ;    here to directly jump to the original interrupt routine.
  182. ;
  183. oldoff    DW    0        ; Room for original timer interrupt offset
  184. oldseg    DW    0        ; Room for original timer interrupt segment
  185.  
  186. paopen    ENDP
  187. ;
  188. ;    int paclose()
  189. ;
  190. ;    This is a C callable function to close the CS:IP grabber and
  191. ;    return the number of CS:IP values grabbed.
  192. ;
  193.     PUBLIC    paclose
  194. paclose    PROC    NEAR
  195.     push    bp
  196.     mov    bp,sp
  197.     push    si
  198.     push    di
  199.     push    es
  200. ;
  201. ;    Disable interrupts while the original interrupt vector is restored.
  202. ;
  203.     cli
  204.     mov    al,TIMER    ; get interrupt number
  205.     mov    ah,25h        ; DOS function = set vector 
  206.     push    ds        ;
  207.     mov    dx,cs:oldoff    ; old timer offset
  208.     mov    ds,cs:oldseg    ; old timer segment
  209.     int    21h        ; restore old vector
  210.     pop    ds        ;
  211. ;
  212. ;    Enable interrupts.
  213. ;
  214.     sti
  215. ;
  216. ;    Calculate the number of CS:IP values
  217. ;
  218.     cmp    bufwrap,0    ; check if the buffer has wrapped
  219.     jne    wrapped        ; jump if it has wrapped
  220.     mov    ax,bufindx    ; no wrap, return buffer index as count
  221.     jmp    done
  222. wrapped: mov    ax,bufsiz    ; wrapped, return buffer size as count
  223. ;
  224. ;    Clean up stack and return
  225. ;
  226. done:    shr    ax,1        ; Return count in number of CS:IP pairs
  227.     shr    ax,1
  228.     pop    es
  229.     pop    di
  230.     pop    si
  231.     pop    bp
  232.     ret
  233.  
  234. paclose    ENDP
  235. _TEXT    ENDS
  236.     END
  237.  
  238.